import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

// 同步
// import Home from '@/views/Menus/Home/Home.vue'
// import Userinfo from '@/views/Menus/User/Userinfo.vue'
// import UserAvatar from '@/views/Menus/User/UserAvatar.vue'
// import UserPwd from '@/views/Menus/User/UserPwd.vue'
// import ArtCate from '@/views/Menus/Article/ArtCate.vue'
// import ArtList from '@/views/Menus/Article/ArtList.vue'
// import Main from '@/views/Main/Main.vue'
// import Reg from '@/views/Reg/Reg.vue'
// import Login from '@/views/Login/Login.vue'

// 异步  只有需要访问的路由的时候才会加载当前路由对应的组件界面
const Home = () => import('@/views/Menus/Home/Home.vue')
const Userinfo = () => import('@/views/Menus/User/Userinfo.vue')
const UserAvatar = () => import('@/views/Menus/User/UserAvatar.vue')
const UserPwd = () => import('@/views/Menus/User/UserPwd.vue')
const ArtCate = () => import('@/views/Menus/Article/ArtCate.vue')
const ArtList = () => import('@/views/Menus/Article/ArtList.vue')
const Main = () => import('@/views/Main/Main.vue')
const Reg = () => import('@/views/Reg/Reg.vue')
const Login = () => import('@/views/Login/Login.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/Reg',
    component: Reg
  },
  {
    path: '/Login',
    component: Login
  },
  {
    path: '/',
    component: Main,
    redirect: '/Home',
    children: [
      {
        path: 'Home',
        component: Home
      },
      {
        path: 'user-info',
        component: Userinfo
      },
      {
        path: 'user-avatar',
        component: UserAvatar
      },
      {
        path: 'user-pwd',
        component: UserPwd
      },
      {
        path: 'art-cate',
        component: ArtCate
      },
      {
        path: 'art-list',
        component: ArtList
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // 如果token失效，且去的页面既不是登录也不是login页面
  if (!store.state.users.token && to.path !== '/Reg' && to.path !== '/Login') {
    // 提醒用户
    Vue.prototype.$message.error('请先登录')
    // 强行跳转至登录页
    return next('/Login')
  }
  next()
})
export default router
